<?php
/**
 * AclResourcesView table data gateway
 *
 * @version $Id: AclResourcesView.php 73 2010-04-25 00:30:44Z yingkuang.chen@gmail.com $
 * @license New BSD License
 * @uses Zend_Db_Table_Abstract
 * @package default
 * @subpackage Model
 */
class Default_Model_DbTable_AclResourcesView extends Zend_Db_Table_Abstract
{

    protected $_name = 'acl_resources_view';


protected $_primary =  array('id');


	public function getById($id)
	{
		$rowSet = $this->find($id);
		return $rowSet->current();
	}

	/**
	 * 取得所有群組
	 *
	 * @param string $parent_id
	 * @return Zend_Table_Rows
	 */
	public function getResource($parent_id='')
	{
		$select = $this->select();

		if (empty($parent_id))
			$select->where('parent_id IS NULL');
		else
			$select->where('parent_id=?',$parent_id);

		$select->order('sort');
		return  $this->fetchAll($select);

	}

	/**
	 * 取得模組分類
	 *
	 */
	public function getResourceKind()
	{
		return $this->fetchAll("kind='menu'",'sort');
	}


	public function getAllResource()
	{
		$arr = $this->_recursiveResource();
		return $arr;
	}

	/**
	 * 取得同一階層-子層數量
	 *
	 * @param string $id
	 * @return array
	 */
	public function getChildCount($id='')
	{
		$where = '';
		if ($id === NULL)
		$where = " WHERE parent_id IS NULL ";
		elseif ($id<>'')
		$where = " WHERE parent_id='$id'";

		$res = $this->getAdapter()->fetchAll("SELECT parent_id,COUNT(*)AS cc FROM acl_resources WHERE parent_id IN(SELECT resource_id FROM acl_resources $where)  GROUP BY parent_id");
		//echo "SELECT parent_id,COUNT(*)AS cc FROM acl_resources WHERE parent_id IN(SELECT resource_id FROM acl_resources $where)  GROUP BY parent_id";
		$arr = array();
		foreach($res as $val)
		{
			$arr[$val['parent_id']] = $val['cc'];
		}

		return $arr;
	}

	public function getChildArr($id)
	{
		return $this->_getChild($id);
	}

	/**
	 * 取得子資源
	 *
	 * @param unknown_type $id
	 * @param unknown_type $arr
	 * @return unknown
	 */
	private function _getChild($id,&$arr=array())
	{

		$where = " WHERE parent_id='$id'";

		$res = $this->getAdapter()->fetchAll("SELECT resource_id FROM acl_resources  $where ");
		foreach($res as $val)
		{
			$arr[] = $val['resource_id'];
			$this->_getChild($val['resource_id'],$arr);

		}
		return $arr;
	}

	/**
	 * 取得父層階層
	 *
	 * @param string $id
	 * @return
	 */
	public function getParents($id='')
	{
		return (array)$this->_recursiveResource($id);
	}

	public function getModuleMenu($id='')
	{
		return $this->_recursiveMenu($id);

	}




	public function getMenuSplit($split='&nbsp;&nbsp;&nbsp;')
	{
		return $this->_recursiveMenu('','',$split);
	}

	public function getMaxSort($id)
	{
		if ($id=='')
		$parent_id = "parent_id IS NULL";
		else
		$parent_id = "parent_id='$id'";

		$res = $this->getAdapter()->fetchRow("SELECT MAX(sort) AS mm FROM acl_resources_view WHERE $parent_id");
		//print_r($res); echo $parent_id;exit;
		return $res['mm'];
	}

	/**
	 * 取得瀏覽選單
	 */
	public function getNavMenu($id)
	{
		$arr = array();

		if ($id <>'')
			$this->_recursivenavMenu($arr,$id);

		return $arr;

	}

	private function _recursivenavMenu(&$arr,$id)
	{

		$res = $this->fetchRow("resource_id='$id'");

		if ($res->parent_id<>''){
			$this->_recursivenavMenu(&$arr,$res->parent_id);
		}
		$arr[$res->resource_id ]= $res->title;
		return $arr;
	}

	private function _recursiveMenu($id,$parent_title='首頁',$split='')
	{
		if ($id =='')
		$res = $this->fetchAll("kind='menu' AND parent_id IS NULL",'sort');
		else
		$res = $this->fetchAll("kind='menu' AND parent_id='$id'",'sort');
		if ($res->count()==0) return false;

		foreach ($res as $val){
			if ($split){
				$title = $split.$parent_title;
				$val->title= $split.$parent_title.$val->title;
				$arr[$val->resource_id] = $val;
			}
			else {
				$title = $parent_title.'/'.$val->title;
				$val->title= $title;
				$arr[$val->resource_id] = $val;
			}
			$temp =  $this->_recursiveMenu($val->resource_id,$title,$split);
			if ($temp)
			$arr = array_merge($arr,$temp);
		}
		return $arr;
	}

	private function _recursiveResource($id)
	{
		if ($id =='')
		$res = $this->fetchAll("parent_id IS NULL",'sort');
		else
		$res = $this->fetchAll(" parent_id='$id'",'sort');
		if ($res->count()==0) return false;
		foreach ($res as $val){
			$arr[$val->id] = $val->toArray();
			$temp =  $this->_recursiveResource($val->id);
			if ($temp) {
				$arr [$val->id]['children']=$temp;
				$arr [$val->id]['children_count']=count($temp);
			}
			else {
				$arr [$val->id]['children']=array();
				$arr [$val->id]['children_count']=0;
			}
		}
		return $arr;
	}


}
